package ncku.account.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ncku.account.data.Category;
import ncku.account.data.GlobalData;
import ncku.account.event.Launcher;

//包含一個TextField和Button
public class categorySel extends JPanel{
	protected JComboBox categorySelItem;
	private JButton categoryEditItem;
	private ImageIcon icon;
	private transactionModule parentPanel;  //儲存母Panel
	
	public categorySel(transactionModule input){
		this.parentPanel = input;  //將母panel儲存
		categorySelItem = getCategoriesJComboBox();  //category選單
		icon = new ImageIcon("edit-icon.png");
		categoryEditItem = new JButton(icon);
		
		//按下"編輯Category"後的Listener(categoryFunction)
		categoryEditItem.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) 
			{ 
				categoryShow categoryShow = new categoryShow();
			}}); 
		
		//adjust size and set layout
        setPreferredSize (new Dimension (140, 30));
        setLayout (null);
		
		categorySelItem.setBounds(0,2,100,25); 
		categoryEditItem.setBounds(105,0,30,30);
		
		add(categorySelItem);
		add(categoryEditItem);
		
	}
	
	public static String[] getCategoriesList(){  //取得最新category list(String array)
    	List<String> result = new ArrayList<String>();
    	for(int i = 0; i < GlobalData.getCategoryNumber(); i++){
			result.add(GlobalData.getCategory(i).getName());
		}
		String [] categorySelItems = new String[result.size()];  //category list
		categorySelItems = result.toArray(categorySelItems);
		return categorySelItems;
    }
	
	JComboBox getCategoriesJComboBox(){  //取得JComboBox
		JComboBox box = new JComboBox(getCategoriesList());
		return box;
	}
	
	//按下Button後會跳出的Frame(show category)
	class categoryShow extends JFrame{
		public JFrame showCategoryFrame;
		
		public categoryShow(){
			showCategoryFrame = new JFrame("類別編輯視窗");
			showCategoryFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
			showCategoryFrame.getContentPane().add (BorderLayout.CENTER,new categoryPanel());
			showCategoryFrame.pack();
			showCategoryFrame.setLocation(350, 150);
			showCategoryFrame.setSize(400, 200);
			showCategoryFrame.setVisible (true);
		}
		public void resetLayout(){  //更新下方的category table
			showCategoryFrame.getContentPane().remove(0);
			showCategoryFrame.getContentPane().add (BorderLayout.CENTER,new categoryPanel());
			showCategoryFrame.validate();
		}
		
		//categoryShow對應的JPanel
		class categoryPanel extends JPanel{
			private JButton JBnew;
			private JButton JBedit;
			private JButton JBdelete;
			private tableFactory categoryTable;
			private int indexAmount = 0;
			
			
			public categoryPanel(){
				JBnew = new JButton("新增類別");
				JBedit = new JButton("編輯類別");
				JBdelete = new JButton("刪除類別");
				
				categoryTable = getCategoriesTable();  //取得下方的table
				categoryTable.setOpaque(true); //content panes must be opaque
				
				JBnew.addActionListener(new ActionListener() {  //按下new category
					public void actionPerformed(ActionEvent e) 
					{ 
						categoryWindows newCategory = new categoryWindows("新增類別",0);
					}}); 
				
				JBedit.addActionListener(new ActionListener() {  //按下edit category
					public void actionPerformed(ActionEvent e) 
					{ 
						 int index = categoryTable.table.getSelectedRow();  //取得當下滑鼠在table的欄位
						categoryWindows editCategory = new categoryWindows("編輯類別",index);
					}}); 
				
				JBdelete.addActionListener(new ActionListener() {  //按下delete category
					public void actionPerformed(ActionEvent e) 
					{ 
						int index = categoryTable.table.getSelectedRow();  //取得當下滑鼠在table的欄位
						GlobalData.deleteCategory(index);
						reset(categoryTable);
					}});
				
				//adjust size and set layout
				setPreferredSize (new Dimension (400, 200));
				setLayout (null);
				
				categoryTable.setBounds(30,50,300,100);
				JBnew.setBounds (20, 20, 100, 25);
				JBedit.setBounds (130, 20, 100, 25);
				JBdelete.setBounds (240, 20, 100, 25);
		    
				add(categoryTable);
				add(JBnew);
				add(JBedit);
				add(JBdelete);
			}
			
			//在show category中按下button(new/edit)後會跳出的Frame
			class categoryWindows extends JFrame{
				private JFrame categoryFrame;
				private String title;  //frame title(new category / edit category)
				private int index;  //點選的index值,若為new category,則index=0
				
				public categoryWindows(String frameTitle, int inputIndex){
					title = frameTitle;
					index = inputIndex;
					
					categoryFrame = new JFrame(title);
					categoryFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
					categoryFrame.getContentPane().add (BorderLayout.CENTER,new categoryWindowsPanel());
					categoryFrame.pack();
					categoryFrame.setLocation(350, 150);
					categoryFrame.setSize(300, 150);
					categoryFrame.setVisible (true);
				}
				
				//categoryWindows對應的JPanel
				class categoryWindowsPanel extends JPanel{
					private JLabel JLcategory;
					private JTextField JTextName;
					private JButton JBSubmit;
					private JButton JBCancel;
					
					public categoryWindowsPanel(){
						JLcategory = new JLabel("類別名稱");
						JTextName = new JTextField (5);
						JBSubmit = new JButton("確定");
						JBCancel = new JButton("取消");
						
						if(title.equalsIgnoreCase("編輯類別")){  //若是按下edit,則記錄滑鼠按下的index值
							JTextName.setText(GlobalData.getCategory(index).getName());
						}
						
						JBSubmit.addActionListener(new ActionListener() {  //按下送出
							public void actionPerformed(ActionEvent e) 
							{ 
								String inputName = JTextName.getText();
								if(inputName.equals("")){  //沒有輸入category name,則跳出警告視窗
									JOptionPane.showMessageDialog(categoryFrame, "你沒有輸入Category名稱！", "警告訊息", JOptionPane.ERROR_MESSAGE);
								}
								else{  //有輸入category name,更新GlobalData-category資料
									if(title.equals("新增類別")){  //新增category
										GlobalData.add(inputName);  //在category資料中寫入新的一筆
										
									}
									else{  //修改對應GlobalData-category
										GlobalData.edit(inputName, index);
									}
									
									categoryFrame.dispose();  //關掉categoryWindows
									reset(categoryTable);
								}
							}}); 
						
						JBCancel.addActionListener(new ActionListener() {  //按下取消
							public void actionPerformed(ActionEvent e) 
							{ 
								JTextName.setText("");
								JTextName.revalidate();
								categoryFrame.dispose();
							}}); 
						
						//adjust size and set layout
						setPreferredSize (new Dimension (120, 100));
						setLayout (null);
						
						JLcategory.setBounds(20, 20, 100, 25);
						JTextName.setBounds (125, 20, 100, 25);
						JBSubmit.setBounds (20, 70, 100, 25);
						JBCancel.setBounds (130, 70, 100, 25);
						
						add(JLcategory);
						add(JTextName);
						add(JBSubmit);
						add(JBCancel);
					}
				}
			}
	}
		//取得category list(Object [][])
		Object[] [] getCategoriesItems(int num){
			Object [][] result = new Object[num][1];
			String [] categoryList = getCategoriesList();
			
			for(int i=0;i<num;i++){
				result[i][0] = categoryList[i];
			}			
			return result;
		}
		
		//製造category table
		tableFactory getCategoriesTable(){
			int indexAmount = GlobalData.getCategoryNumber();
			tableFactory table;
			
			Object [] [] CategoryItems = getCategoriesItems(indexAmount);  //顯示在下方Table的Category object
			String [] columnItems = {"類別"};  //Category Table的項目
			table = new tableFactory(columnItems,CategoryItems);
			
			return table;
		}
		
		void reset(tableFactory categoryTable){
			categoryTable = getCategoriesTable();  //更新categoryItem(in table)
			categorySelItem = getCategoriesJComboBox();  //更新categoryJComboBox
			
			categoryTable.updateUI();  
			resetLayout();//重畫table
			
			categorySelItem.updateUI();
			parentPanel.resetLayoutBox();  //重畫category JComboBox
		}
	}
}
